/* ks */

package model;

import java.util.ArrayList;
import java.util.Date;
import javax.swing.DefaultListModel;
import javax.swing.ListModel;

/**
 * This class represents an Auction or a requested auction in the Auction Central
 * system.
 * @author Kevin
 * @version 1.0
 * @date Winter 2014
 */
public class Auction {
	/** auction numbers will begin at this number */
	private static final int BASE_AUCTION_NUMBER = 10000;

	/** number of auctions that have been held */
	private static int num_of_auctions = 0;

	/** Holds all the items that belong to this auction */
	private ArrayList<Item> my_items = new ArrayList<Item>();
	
	
	private DefaultListModel<Item> my_list_model = new DefaultListModel<Item>();

	/** The date the auction will occur */
	private Date my_date;

	/** Unique auction identifier */
	private int my_auction_number;

	/** The time the auction will begin */
	private TimeObject my_auction_time;

	/** The non profit who is doing this auction */
	private NonProfit my_owner;

	/** Is this auction ready to go? */
	private boolean is_live = false;

	private int my_duration;
	
	private int requested_num_items;



	//private int my_numberOfItems; //use list.size()

	/* ************************************************ */

	/**
	 * Construct a new auction. USE THIS ONE for hard-adding a new auction
	 * 
	 * @pre the_date != null && is today's date or after
	 * @pre the_start != null
	 * @pre the_end != null
	 * @pre the_owner != null && does not already have another live auction
	 * @post object is created
	 * 
	 * @param the_date
	 *            Date Auction will occur
	 * @param the_start
	 *            Start time of the Auction
	 * @param the_end
	 *            End time of the Auction
	 * @param the_owner
	 *            Non Profit responsible for the Auction.
	 */
	public Auction(final Date the_date, final TimeObject the_time,
			final TimeObject the_end, final NonProfit the_owner) {
		setDate(the_date);
		setTime(the_time);
		my_owner = the_owner;
		my_auction_number = num_of_auctions + BASE_AUCTION_NUMBER;
		num_of_auctions++;
	}

	/* ************************************************ */

	/**
	 * USE THIS ONE for new auction requests
	 * @pre the_date valid date in the present or future
	 * @pre the_time != null
	 * @pre the_duration != null
	 * @pre requested_items != null
	 * @pre the_owner != null
	 * @post object is created
	 * 
	 * @param the_date
	 * @param the_time
	 * @param the_duration
	 * @param requested_items
	 * @param the_owner
	 */
	public Auction(Date the_date, TimeObject the_time, int the_duration,
			final int requested_items, NonProfit the_owner) {
		setDate(the_date);
		setTime(the_time);
		setDuration(the_duration);
		requested_num_items = requested_items;
		my_owner = the_owner;
		my_auction_number = num_of_auctions + BASE_AUCTION_NUMBER;
		num_of_auctions++;
	}
	
	/** Get the amount of items the requested auction will contain
	 * This is not valid for LIVE auctions
	 * @return requested number of items
	 */
	public int getRequestedAmount() {
		return requested_num_items;
	}

	/**
	 * Run at start of system launch to reset for File I/O.  In a real system
	 * this would not be happening.
	 */
	public static void reset() {
		num_of_auctions = 0;
	}
	
	/**
	 * Get the number of items in a LIVE auction
	 * @return size of item list
	 */
	public int getNumOfItems() {
		return my_items.size();
	}

	public Date getAuctionDate() {
		return my_date;
	}
	
	/**
	 * Get the unique auction number
	 * @return the unique auction identifier
	 */
	public int getAuctionNumber() {
		return my_auction_number;
	}

	/**
	 * Get the NonProfit hosting this auction
	 * @return the my_owner nonprofit
	 */
	public NonProfit getOwner() {
		return my_owner;
	}

	/**
	 * The time the auction should be ended
	 * @return the my_end_time string format
	 */
	public String getEndTime() {
		return my_auction_time.getEndTime();
	}
	
	/** Returns the ListModel version of the data */
	public ListModel<Item> getListModel() {
		return (ListModel<Item>)my_list_model;
	}
	
	

	/**
	 * @pre the_time is valid
	 * Set the time of the auction's start
	 * @param the_time TimeObject
	 */
	public void setTime(final TimeObject the_time) {
		my_auction_time = the_time;
	}

	/**
	 * @return the my_start_time TimeObject
	 */
	public String getStartTime() {
		return my_auction_time.getStartTime();
	}

	/**
	 * @return the my_start_time TimeObject
	 */
	public TimeObject getStartTimeObject() {
		return my_auction_time;
	}

	/**
	 * Get the date the auction is scheduled
	 * @return the my_date Date object
	 */
	public Date getDate() {
		return my_date;
	}

	/**
	 * @pre my_date is a valid Date
	 * @param my_date
	 *            the my_date to set
	 */
	public void setDate(Date my_date) {
		this.my_date = my_date;
	}

	/**
	 * Add an item to the Auction
	 * @pre add_me != null and is not in any other auction
	 * @param add_me
	 *            the Item to be added
	 */
	public void addItem(final Item add_me) {
		my_items.add(add_me);
		my_list_model.addElement(add_me);
	}

	/**
	 * Deletes an item from the Auction
	 * @pre delete_me exists in this auction
	 * @param delete_me
	 *            Item to be removed
	 */
	public void removeItem(final Item delete_me) {
		my_items.remove(delete_me);
		my_list_model.removeElement(delete_me);
	}
	
	/**
	 * DO NOT USE Finds the item of the requested item number.
	 * null if not found
	 * @param item_number
	 * @return the requested item or null if not found
	 */
	public Item findItem(int item_number) {
		Item result = null;
		for(int i = 0; i < my_items.size(); i++) {
			if (my_items.get(i).getMyItemNumber() == item_number) {
				result = my_items.get(i);
			}
		}
		return result;
	}

	/**
	 * Retrieve the auction's item list.
	 * 
	 * @return the my_items
	 */
	public ArrayList<Item> getItemList() {
		return my_items;
	}

	/**
	 * Requested auctions are not live until approved.
	 * @return the is_live
	 */
	public boolean isLive() {
		return is_live;
	}

	/**
	 * @pre this auction has been added to an AuctionDay object in the AuctionCalendar
	 * @post the auction will be seen as a live auction
	 * @param is_live
	 *            the is_live to set
	 */
	public void setLive(boolean is_live) {
		this.is_live = is_live;
	}

	/**
	 * The time between auction start and auction end
	 * @param the_duration
	 */
	public void setDuration(int the_duration) {
		my_duration = the_duration;
	}

	/** The time between auction start and auction end
	 * 
	 * @return
	 */
	public int getDuration() {
		return my_duration;
	}
	
	/**
	 * Auction in string format delimited with ~
	 */
	@SuppressWarnings("deprecation")
	public String toString() {
		return (my_date.getYear() + 1900) + "~" + my_date.getMonth() + "~" + my_date.getDate() + "~" + 
				my_auction_time.getStartHour() + "~" + my_auction_time.getStartMinute() + "~" + 
				0 + "~" + 0 + "~" + 0 + "~" + 0 + "~nonprofitObject";
	}

	/**
	 * Get the Donyina-format name of the auction
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public String getName() {
		String result = "";
		if (getOwner() != null) {
			result = getOwner().getMy_organization().replace(" ", "-") + "-" + (my_date.getMonth() + 1)
			+ "-" + my_date.getDate() + "-" + (my_date.getYear() + 1900);
		}
		return result;
	}

	/**
	 * This is to return an auction from the list.....Not sure if it's really needed
	 * @param my_selectedAuction
	 * @return
	 */
	public Auction getAuction(Auction my_selectedAuction) {
		//search auction list and return the auction
		return my_selectedAuction;
		
	}

}
